define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    is_publish:0,
                    step:1,
                    pid:0,
                    bankList:[],
                    bankSubbranchList:[],
                    contactline:'',
                    mchCacheData:{},
                    industryList:[],
                    accountType:'01',
                    type:1,
                    show_dialog_alert:0,
                    FeeValueList:[],
                    store_id:0,
                    editData:{},
                    uploadUrl:app.params.apiDomain+"apiv2/mch_my_bank/upload"
                };
            },
            el: '#pageMchAdd',
            created: function() {
               if(this.$route.params.store_id>0){
                  this.store_id = this.$route.params.store_id;
                  this.getMerchantData();
                }else{
                  // console.log(this.$route.params.store_id);
                  this.getMchAddCacheData();
                }
                this.fetchData();
            },


            mounted: function() {
                // console.log("123");
                if (this.mounted == 1) {
                    return;
                }
                var _this = this;
                
                // app.area(".DestinationArea");
                // app.area(".bankArea");
                app.upload("#mainPhoto");
                app.upload("#IndustryLicensePhoto");
                app.upload("#licensePhoto");
                app.upload("#certPhotoA");
                 app.upload("#ShopEntrancePhoto");
                 app.upload("#ContractPhoto");
                
                app.upload("#certPhotoB",function(){
                    window.photo.uploadUrl = _this.uploadUrl;
                });

                 app.cityPicker("#DestinationArea",{
                        title: "选择地区",
                        onChange: function (picker, values, displayValues) {
                          console.log( values);
                          //   console.log( displayValues);
                            var editData = _this.editData;
                            _this.editData={};
                            _this.destinationAreaValues = values;
                            _this.destinationArea = displayValues;
                            editData.City = values[1];
                            editData.District = values[2];
                            editData.Province = values[0];
                            editData.city_name = displayValues[1];
                            editData.county_name = displayValues[2];
                            editData.province_name = displayValues[0];
                            editData.DestinationArea = displayValues.join(" ");
                            _this.editData = editData;

                            console.log( _this.editData);
                            
                        }
                });

                  app.cityPicker("#bankArea",{
                        title: "选择地区",
                        onChange: function (picker, values, displayValues) {
                          // console.log( values);
                          //   console.log( displayValues);
                          var editData = _this.editData;
                            _this.editData={};
                            
                           var oldBranchCityName = editData.BranchCityName;
                            _this.bankAreaValues = values;
                            editData.bankArea = displayValues.join(" "); 
                            editData.BranchCityName = displayValues[1];
                            editData.BranchProvince = values[0];
                            editData.BranchCity = values[1];
                            _this.editData = editData;
                             if( oldBranchCityName != displayValues[1]){
                              _this.getBankSubbranch();
                            }
                            // $("#BranchProvince").val(values[0]);
                            // $("#BranchCity").val(values[1]);
                           
                        }
                });
                 
                this.mounted = 1;

            },
            methods: {
                fetchData: function() {
                    var _this = this;
                    this.getBank();
                    this.getIndustryList();
                    if( this.$route.params.type==3){
                         this.accountType='02';
                    }
                    this.pid=this.$route.params.pid;
                    this.getFeeValueList();
                    this.type = this.$route.params.type;
                    // this.accountType = this.$route.params.type==2?'2':'1';
                } ,
                hide_dialog_alert:function(){
                    this.show_dialog_alert=0;
                },
                dialog_alert:function(){
                    this.show_dialog_alert=1;
                },
               getBank:function(){
                    var that = this;
                    app.get("apiv2/bank/getBank", {
                      
                    }, function (res) {
                      
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.bankList = res.data;
        
                    });
                },
                getMerchantData:function(){
                    var that = this;
                    app.get("apiv2/mch_my_bank/merchantData", {
                      store_id : this.store_id
                    }, function (res) {   
                      if (res.code != 200) {
                         return ;
                      }
                      if(res.data){
                          that.editData = res.data;
                          that.contactline = res.data.Contactline;
                          that.accountType = res.data.AccountType;
                          Vue.nextTick(function(){
                            that.getBankSubbranch();
                          });
                          

                      }
                    });
                },
                setVal:function(e){
                  console.log(e.target.value);
                  this.editData[e.target.name] = e.target.value;
                },
                
                getFeeValueList:function(){
                    var that = this;
                    app.get("apiv2/mch_my_bank/getFeeList", {
                      pid:this.pid
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.FeeValueList = res.data;
        
                    });
                },
                getBankSubbranch:function(){
                    var that = this;
                    var data={};
                     
                    data.bankId=this.editData.BranchId;
                    data.city_name=this.editData.BranchCityName;
        
                    app.get("apiv2/bank/getBankSubbranch", data, function (res) {
                      
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.bankSubbranchList = res.data;
                      
                    });
                },
                getIndustryList:function(){
                    var that = this;
                     
                     that.industryList = [{
                        industryId:'4015063000020189',
                        industryName:'物业缴费'
                     },{
                        industryId:'2015050700000000',
                        industryName:'美食'
                     },{
                        industryId:'2015091000052157',
                        industryName:'超市便利店'
                     },{
                        industryId:'2015062600004525',
                        industryName:'休闲娱乐'
                     },{
                        industryId:'2015062600002758',
                        industryName:'购物'
                     },{
                        industryId:'2016062900190124',
                        industryName:'爱车'
                     },{
                        industryId:'2015063000020189',
                        industryName:'生活服务'
                     },{
                        industryId:'2016062900190296',
                        industryName:'医疗健康'
                     },{
                        industryId:'2016042200000148',
                        industryName:'教育培训'
                     },{
                        industryId:'2015080600000001',
                        industryName:'航旅'
                     },{
                        industryId:'2016062900190337',
                        industryName:'专业销售/批发'
                     },{
                        industryId:'2016062900190371',
                        industryName:'政府/社会组织'
                     }];
                     that.industryid = $("[name='industryId']").attr("industryId");
                },
                setContactLine:function(){
                        var value = $("[name='bankName']").val();
                        this.contactline=$("[name='bankName']").find("[value='"+value+"']").attr("contactline");

                        this.editData.bankName = value;
                },
                pevStep:function(){
                     
                    if(this.step>1){
                        this.step = this.step-1;
                    }
                },
                publish: function() {
                    var _this = this;
                    if (_this.is_publish === 1) {
                        return;
                    }
                     _this.is_publish = 1;
                     
                    var data = $("#pageMchAdd").find("#mchAddForm").serializeArray();
                    data.push({
                        name:"pid",
                        value:this.$route.params.pid
                    });
                    data.push({
                        name:"MerchantType",
                        value:"0"+this.$route.params.type
                    });

                    data.push({
                        name:"method",
                        value:'enterNo'
                    });
                     data.push({
                        name:"CardHolderAddress",
                        value:$("#BranchProvince option").eq($("#BranchProvince").attr("selectedIndex")).text()+$("#bankCity option").eq($("#bankCity").attr("selectedIndex")).text()
                    });
                    
                    
                    data.push({
                        name:"BranchName",
                        value:$("#BranchId option").eq($("#BranchId").attr("selectedIndex")).text()
                    });
                      

                    app.post("apiv2/mch_my_bank",data,function(res){
                       _this.is_publish =0;
                        if(res.code!=200){
                            app.alert(res.msg);
                            return;
                        }
                        app.setPageData("index",{reload:1});
                        app.alert("恭喜，提交成功!",function(){
                             _this.$router.push({ name: 'index',params:{
                                store_id:res.data.store_id,
                                pid:_this.pid
                             }});
                        });
                         // _this.$router.push({ name: 'msgSuccess'});
                         // _this.$router.push();

                    });
                    

                },
                getCode:function(){
                      var i =60;
                      var that = this;
                      // var that = $("#codeBnt");
                      if(this.sendingcode==1){
                         return ;
                      }
                      this.sendingcode = 1;
                     //验证码倒计时
                      $("#codeBnt").text("重新获取("+i+"s)");
                      var t = setInterval(function(){
                         i--;
                         $("#codeBnt").text("重新获取("+i+"s)");
                         if(i===0){
                            clearInterval(t);
                            $("#codeBnt").text('获取验证码');
                            that.sendingcode =0;
                         }
                        },1000);
                      //同步返回结果：
                      
                      var phone = $("#pageMchAdd").find("[name='PrincipalMobile']").val();

                      //加密
                     
                      app.get("apiv2/mch_my_bank",{phone:phone,method:'sendMsCode',BizType:'04'},function(res){
                         if(res.code!=200){
                            app.alert(res.msg);
                            that.sendingcode = 0;
                            clearInterval(t);
                            $("#codeBnt").text('获取验证码');
                           return ;
                         }
                      });

                    },
                mchAddCacheData:function(){
                    var mchAddData =  $("#pageMchAdd").find("#mchAddForm").serializeArray();
                    app.cache().set("mchAddData",mchAddData);
                },

                getMchAddCacheData:function(){
                     var mchAddData = app.cache().get("mchAddData");
                     this.mchAddData = mchAddData;
                     // var editData = this.editData;
                     var editData ={};
                     for (var i in mchAddData) {
                         var name = mchAddData[i]['name'];
                          // console.log(name);
                          // console.log(mchAddData[i]['value']);
                         editData[name]=mchAddData[i]['value'];
                         // console.log(this.editData);
                         if(mchAddData[i]['value']){
                            $("[name='"+name+"']").val(mchAddData[i]['value']);
                         }
                         if(name=="bankId"){
                             $("[name='"+name+"']").attr("bankId",mchAddData[i]['value']);
                         }
                         if(name=="industryId"){
                             this.industryid =mchAddData[i]['value'];
                         }

                       
                     }
                     this.editData = editData;
                     
                     if(editData.BranchId && editData.BranchCityName){
                      this.getBankSubbranch();
                     }
                     // console.log( "getMchAddCacheData",this.editData);
                     
                     
                },
                 
                nextStep:function(){
                    // 校验是否可以下一步
                    if(this.step==1){
                        if($("[name='PrincipalPerson']").val()===""){
                            app.alert("请输入负责人姓名");
                            return ;
                        }
                        if($("[name='PrincipalCertNo']").val()===""){
                            app.alert("请输入负责人身份证号");
                            return ;
                        }
                        if($("[name='PrincipalMobile']").val()===""){
                            app.alert("请输入负责人手机号");
                            return ;
                        }
                    }
                    // 校验是否可以第二步
                     if(this.step==2){
                        if($("[name='MerchantName']").val()===""){
                            app.alert("请输入商户名称");
                            return ;
                        }
                        if($("[name='Alias']").val()===""){
                            app.alert("请输入商户简称");
                            return ;
                        }
                        
                        if($("[name='industryId']").val()===""){
                            app.alert("请选择行业类别");
                            return ;
                        }
                        if($("[name='Address']").val()===""){
                            app.alert("请输入详细地址");
                            return ;
                        }
                        if($("[name='Province']").val()==="" || $("[name='City']").val()==="" || $("[name='District']").val()===""){
                            app.alert("请选择省市区");
                            return ;
                        }
                        if($("[name='CustomerPhone']").val()===""){
                            app.alert("请输入客服电话");
                            return ;
                        }
                    }
                     // 校验是否可以第三步
                     if(this.step==3){
                        if($("[name='BankCardNo']").val()===""){
                            app.alert("请输入银行卡号");
                            return ;
                        }
                        if($("[name='BankCertName']").val()===""){
                            app.alert("请输入开户人");
                            return ;
                        }
                        if($("[name='bankId']").val()===""){
                            app.alert("请选择银行");
                            return ;
                        }
                        if($("[name='BranchProvince']").val()==="" || $("[name='BranchCity']").val()===""){
                            app.alert("请选择银行省市区");
                            return ;
                        }
                        if($("[name='bankName']").val()===""){
                            app.alert("请选择支行");
                            return ;
                        }
                        
                    }
                     // 校验是否可以第四步
                     if(this.step==4){
                        // if($(".imgList-name").length===0){
                        //     app.alert("请上传身份证");
                        //     return ;
                        // }
                        this.mchAddCacheData();
                        this.publish();
                     }
                    
                    if(this.step<4){
                        this.step = this.step+1;
                        this.mchAddCacheData();
                        
                    }
                }

               
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
